﻿using BCL.Tool;
using DevicesView.Logic.User;
using System.Windows;
using DevicesView.Logic.App;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using XiaonuanPMS.Logic;
namespace WpfControlTool.Windows
{
    /// <summary>
    /// WindowLogin2.xaml 的交互逻辑
    /// </summary>
    public partial class WindowLogin3 : Window
    {
        private List<UserConfig> users;
        private XiaonuanPMS.Entity.RuiPaiPMSOrgInfo orginfo;
        public WindowLogin3()
        {
            InitializeComponent();

            users = UserConfig.GetLoginUserNames();

            //TextBoxName.TextChanged += TextBoxName_TextChanged;
            if (AppResource.CurrentResource.LoginUser != null)
                this.TextBoxName.Text = AppResource.CurrentResource.LoginUser.LoginName;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string msg = null;
            if (string.IsNullOrEmpty(TextBoxPass.Password))
                msg = "请填写密码".Language();
            if (string.IsNullOrEmpty(TextBoxName.Text))
                msg = "请填写用户名".Language();
            if (orginfo == null)
                msg = "请选择登录医院".Language();
            if (msg != null)
            {
                TextBoxMsg.Text = msg;
                return;
            }
            TestingsLogic userLogic = new TestingsLogic();
            string error = userLogic.Login(TextBoxName.Text, TextBoxPass.Password, orginfo.orgcode, checkbox1.IsChecked == true);
            if (string.IsNullOrEmpty(error))
                DialogResult = true;
            else
                TextBoxMsg.Text = error;
        }

        private void TextBoxPass_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (TextBoxPass.Password.Trim() == "")
                TextBlockpassTip.Text = "请输入密码".Language();
            else
                TextBlockpassTip.Text = "";
        }

        private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            base.DragMove();
        }

        private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        private void TextBoxName_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            string name = TextBoxName.Text.Trim();

            StackPanelOrgs.Children.Clear();
            checkbox1.IsChecked = false;
            TextBoxOrg.Text = "";
            TextBoxPass.Password = "";
            orginfo = null;

            try
            {
                TestingsLogic tl = new TestingsLogic();
                List<XiaonuanPMS.Entity.RuiPaiPMSOrgInfo> orgs = tl.GetOrgs(name);

                if (orgs != null && orgs.Count() > 0)
                    foreach (var item in orgs)
                        StackPanelOrgs.Children.Add(new TextBlock { Text = item.orgname, Tag = item });

                UserConfig user = users.FirstOrDefault(o => o.LoginName == name);
                if (user == null)
                    return;

                if (user.Remember)
                {
                    checkbox1.IsChecked = true;
                    TextBoxPass.Password = user.Password;
                }
                orginfo = orgs.FirstOrDefault(o => o.orgcode == user.LoginOrgId);
                if (orginfo != null)
                    TextBoxOrg.Text = orginfo.orgname;
            }
            catch (System.Exception ex)
            {
                BCL.BaseLib.Basics.BCLApplication.log?.Error(ex);
            }


        }


        private void TextBoxOrg_GotFocus(object sender, RoutedEventArgs e)
        {
            InkCanvasOrgs.Visibility = Visibility.Visible;
        }

        private void TextBlock_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            orginfo = ((TextBlock)sender).Tag as XiaonuanPMS.Entity.RuiPaiPMSOrgInfo;
            TextBoxOrg.Text = orginfo.orgname;
            InkCanvasOrgs.Visibility = Visibility.Collapsed;
        }

        private void TextBoxName_LostFocus(object sender, RoutedEventArgs e)
        {
            StackPanelOrgs.Children.Clear();
            TestingsLogic tl = new TestingsLogic();
            List<XiaonuanPMS.Entity.RuiPaiPMSOrgInfo> orgs = tl.GetOrgs(TextBoxName.Text.Trim());

            if (orgs == null || orgs.Count() == 0)
                return;
            foreach (var item in orgs)
                StackPanelOrgs.Children.Add(new TextBlock { Text = item.orgname, Tag = item });
        }

        private void TextBlock_MouseUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            InkCanvasOrgs.Visibility = InkCanvasOrgs.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
